<ul class="breadcrumb">
    <li class="active">管理机构信息&nbsp;|&nbsp;</li>
    <li ><?php
        echo $control->getAjaxLink(array(
            'id' => 'showRelationshipList',
            'name' => '管理机构关系'
        ));
        ?>
    </li>
</ul>
<?php
$space = Blyn::app()->getActiveSpace();
$model = new BDynamicFormModel();
$model->addField('myId', NULL, 'Specify Unique Id(domain or email):');
$model->addField('spaceId', NULL, 'Space Id:');
$model->addField('spaceName', NULL, 'Space Name:');
$model->addField('dbRepository', NULL, 'Choose Db Repository:');
$isMyIdExist = TRUE;
$isSaved = FALSE;

$screen = Blyn::app()->getUIManager()->getCurrentScreen();
//$sender = Blyn::app()->getUIManager()->getSenderControl();

$spaceCooker = new BSpaceCooker();
$currentSpaceInfo = $spaceCooker->getCurrentSpaceInfo();

if ($screen->isAjaxLinkClicked('modifySpace')) {
    if (!empty($_POST) && isset($_POST['BDynamicFormModel'])) {
        $model->attributes = $_POST['BDynamicFormModel'];
        //$space->id = $model->spaceId;
        $space->myId = $model->myId;
        $space->name = $model->spaceName;
        $space->save();
        $spaceCooker->addSpaceInfo(array('myId' => $model->myId, 'name' => $model->spaceName, 'spaceId' => $space->getId()));
        $isSaved = TRUE;
    }
}

if ($screen->isAjaxLinkClicked('saveRepository')) {
    if (!empty($_POST) && isset($_POST['BDynamicFormModel'])) {
        $model->attributes = $_POST['BDynamicFormModel'];
        //$space->id = $model->spaceId;
        $spaceRepository = $model->dbRepository;
        if ($spaceRepository != NULL) {
            $oSpaceInfo = $currentSpaceInfo;
            if ($oSpaceInfo != NULL) {
                $oSpaceInfo->addItem('dbRepository', $spaceRepository);
                $isSavedRepository = TRUE;
                //return $oSpaceInfo;
            }
        } else {
            $model->addError('dbRepository', 'please choose repository!');
        }
    }
}

if ($screen->isAjaxLinkClicked('checkMyId')) {
    if (!empty($_POST) && isset($_POST['BDynamicFormModel'])) {
        $model->attributes = $_POST['BDynamicFormModel'];
        $result = BSpace::findById($model->myId);
        if ($result instanceof BSpace)
            $model->addError('myId', 'Same specify Id exist!');
        else {
            $isMyIdExist = FALSE;
        }
    }
}

$model->spaceId = $space->getId();
$model->spaceName = $space->getName();
if ($space->myId != NULL && $model->myId == NULL)
    $model->myId = $space->myId;
$savedRepository = $spaceCooker->getSpaceInfoItemValue('dbRepository');
if ($savedRepository != NULL) {
    $model->dbRepository = $savedRepository;
}
?>
<?php echo CHtml::beginForm(); ?>
<div>
    <h5 class="text-error"><?php echo CHtml::errorSummary($model); ?></h5>
    <?php if ($isSaved) { ?>
        <div class="well text-danger">Save successfully!</div>
    <?php } ?>
</div>
<table class="table">
    <tr>
        <td>
            <div class="form">

                <div class="row">
                    <?php echo CHtml::activeLabel($model, 'spaceId'); ?>
                    <?php echo CHtml::activeTextField($model, 'spaceId', array('disabled' => TRUE)); ?>
                </div>

                <div class="row">
                    <?php echo CHtml::activeLabel($model, 'myId'); ?>
                    <?php echo CHtml::activeTextField($model, 'myId'); ?>       
                    <?php echo $screen->getAjaxLink(array('name' => yii::t('site', 'Check'), 'id' => 'checkMyId'), array('htmlOptions' => array('class' => 'btn'))); ?>       
                    <?php if (!$isMyIdExist) { ?>
                        <div class="text-success">ID is not used, you can choose it!</div>
                    <?php } ?>
                </div>

                <div class="row">
                    <?php echo CHtml::activeLabel($model, 'spaceName'); ?>
                    <?php echo CHtml::activeTextField($model, 'spaceName') ?>
                </div>  

                <div class="row submit">
                    <?php echo $screen->getAjaxLink(array('name' => yii::t('site', 'Modify'), 'id' => 'modifySpace'), array('htmlOptions' => array('class' => 'btn'))); ?>       
                </div>    
            </div>
        </td>
        <td>
            <div class="form">

                <div class="row">
                    <?php
                    $dbRepositoryList = array('0' => '请选择');
                    $dbRepositoryList += $spaceCooker->getSpaceRepositoryList();
                    echo CHtml::activeLabel($model, 'dbRepository');
                    echo CHtml::activeDropDownList($model, 'dbRepository', $dbRepositoryList);
                    ?>
                </div>               

                <div class="row submit">
                    <?php echo $screen->getAjaxLink(array('name' => yii::t('site', 'Save'), 'id' => 'saveRepository'), array('htmlOptions' => array('class' => 'btn'))); ?>       
                </div> 
                <?php if (isset($isSavedRepository) && $isSavedRepository) { ?>
                    <div class="well text-success">saving space repository is done!</div>
                <?php } ?>
            </div><!-- form -->
        </td>
    </tr>
</table>
<?php echo CHtml::endForm(); ?>
